package org.gjy.m8.concurrent.lambda;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.ObjectUtils;
import org.gjy.m8.concurrent.lambda.entity.BankWtczRequestBo;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-10-29 14:04:47
 */
public class Lam1Test {
    private static final Logger log = LoggerFactory.getLogger(Lam1Test.class);

    @Test
    public void test1() {
        List<BankWtczRequestBo> list = initData();
        Map<String, List<BankWtczRequestBo>> collect = list.stream().collect(Collectors.groupingBy(e -> e.getRequestSrl().substring(0, 16)));
        collect.forEach((k, v) -> {
            String queryResult = "{\"OUTPUTCOLLECTION\":{\"OUTPUTCOLLECTION_ITEM\":[{\"OR_BATCH_NO\":\"TSZF2025102815852458\",\"OR_SUB_DATE\":\"2025-10-28\",\"OR_COLLECTION_TYPE\":\"2\",\"SETTLEMENT_NO\":\"TSZF-20251028-000005\",\"PAYMENT_STATE\":\"-2\",\"MESSAGE\":\"第6、7、15、25、26、46、52行银行付款失败。\",\"TOTAL_NUM\":55,\"TOTAL_AMT\":25461.46,\"ACT_TOTAL_NUM\":48,\"ACT_TOTAL_AMT\":22786.36,\"OR_ORDER_NO\":\"null\",\"OR_TRADE_DATA\":{\"OR_TRADE_DATA_ITEM\":[{\"OR_SERIAL_NO\":\"1\",\"OR_PAYMENT_NO\":\"null\",\"PAYMENT_STATE\":\"3\",\"MESSAGE\":\"处理成功,FSCI0000\",\"AMT\":300.0,\"ACT_AMT\":300.0,\"ACT_PAYMENT_DATE\":\"2025-10-28T17:33:17\"},{\"OR_SERIAL_NO\":\"2\",\"OR_PAYMENT_NO\":\"null\",\"PAYMENT_STATE\":\"3\",\"MESSAGE\":\"处理成功,FSCI0000\",\"AMT\":60.0,\"ACT_AMT\":60.0,\"ACT_PAYMENT_DATE\":\"2025-10-28T17:33:19\"},{\"OR_SERIAL_NO\":\"3\",\"OR_PAYMENT_NO\":\"null\",\"PAYMENT_STATE\":\"3\",\"MESSAGE\":\"处理成功,FSCI0000\",\"AMT\":200.0,\"ACT_AMT\":200.0,\"ACT_PAYMENT_DATE\":\"2025-10-28T17:33:21\"},{\"OR_SERIAL_NO\":\"4\",\"OR_PAYMENT_NO\":\"null\",\"PAYMENT_STATE\":\"3\",\"MESSAGE\":\"处理成功,FSCI0000\",\"AMT\":180.0,\"ACT_AMT\":180.0,\"ACT_PAYMENT_DATE\":\"2025-10-28T17:33:22\"},{\"OR_SERIAL_NO\":\"5\",\"OR_PAYMENT_NO\":\"null\",\"PAYMENT_STATE\":\"3\",\"MESSAGE\":\"处理成功,FSCI0000\",\"AMT\":100.0,\"ACT_AMT\":100.0,\"ACT_PAYMENT_DATE\":\"2025-10-28T17:33:24\"},{\"OR_SERIAL_NO\":\"6\",\"OR_PAYMENT_NO\":\"null\",\"PAYMENT_STATE\":\"-1\",\"MESSAGE\":\"账号状态异常,FSCO6611\",\"AMT\":560.0,\"ACT_AMT\":0,\"ACT_PAYMENT_DATE\":\"2025-10-28T17:33:25\"},{\"OR_SERIAL_NO\":\"7\",\"OR_PAYMENT_NO\":\"null\",\"PAYMENT_STATE\":\"-1\",\"MESSAGE\":\"账号状态异常,FSCO6611\",\"AMT\":40.0,\"ACT_AMT\":0,\"ACT_PAYMENT_DATE\":\"2025-10-28T17:33:35\"},{\"OR_SERIAL_NO\":\"8\",\"OR_PAYMENT_NO\":\"null\",\"PAYMENT_STATE\":\"3\",\"MESSAGE\":\"处理成功,FSCI0000\",\"AMT\":250.0,\"ACT_AMT\":250.0,\"ACT_PAYMENT_DATE\":\"2025-10-28T17:33:37\"},{\"OR_SERIAL_NO\":\"9\",\"OR_PAYMENT_NO\":\"null\",\"PAYMENT_STATE\":\"3\",\"MESSAGE\":\"处理成功,FSCI0000\",\"AMT\":750.0,\"ACT_AMT\":750.0,\"ACT_PAYMENT_DATE\":\"2025-10-28T17:33:39\"},{\"OR_SERIAL_NO\":\"10\",\"OR_PAYMENT_NO\":\"null\",\"PAYMENT_STATE\":\"3\",\"MESSAGE\":\"处理成功,FSCI0000\",\"AMT\":550.0,\"ACT_AMT\":550.0,\"ACT_PAYMENT_DATE\":\"2025-10-28T17:33:40\"},{\"OR_SERIAL_NO\":\"11\",\"OR_PAYMENT_NO\":\"null\",\"PAYMENT_STATE\":\"3\",\"MESSAGE\":\"处理成功,FSCI0000\",\"AMT\":250.0,\"ACT_AMT\":250.0,\"ACT_PAYMENT_DATE\":\"2025-10-28T17:33:42\"},{\"OR_SERIAL_NO\":\"12\",\"OR_PAYMENT_NO\":\"null\",\"PAYMENT_STATE\":\"3\",\"MESSAGE\":\"处理成功,FSCI0000\",\"AMT\":500.0,\"ACT_AMT\":500.0,\"ACT_PAYMENT_DATE\":\"2025-10-28T17:33:43\"},{\"OR_SERIAL_NO\":\"13\",\"OR_PAYMENT_NO\":\"null\",\"PAYMENT_STATE\":\"3\",\"MESSAGE\":\"处理成功,FSCI0000\",\"AMT\":75.0,\"ACT_AMT\":75.0,\"ACT_PAYMENT_DATE\":\"2025-10-28T17:33:45\"},{\"OR_SERIAL_NO\":\"14\",\"OR_PAYMENT_NO\":\"null\",\"PAYMENT_STATE\":\"3\",\"MESSAGE\":\"处理成功,FSCI0000\",\"AMT\":150.0,\"ACT_AMT\":150.0,\"ACT_PAYMENT_DATE\":\"2025-10-28T17:33:47\"},{\"OR_SERIAL_NO\":\"15\",\"OR_PAYMENT_NO\":\"null\",\"PAYMENT_STATE\":\"-1\",\"MESSAGE\":\"账户余额不足支付,FSCO6603\",\"AMT\":495.0,\"ACT_AMT\":0,\"ACT_PAYMENT_DATE\":\"2025-10-28T17:33:48\"},{\"OR_SERIAL_NO\":\"16\",\"OR_PAYMENT_NO\":\"null\",\"PAYMENT_STATE\":\"3\",\"MESSAGE\":\"处理成功,FSCI0000\",\"AMT\":550.0,\"ACT_AMT\":550.0,\"ACT_PAYMENT_DATE\":\"2025-10-28T17:33:50\"}]}}]}}";
            JSONObject root = JSON.parseObject(queryResult);
            JSONObject outputcollection = root.getJSONObject("OUTPUTCOLLECTION");
            JSONArray outs = outputcollection.getJSONArray("OUTPUTCOLLECTION_ITEM");
            if (!CollectionUtil.isEmpty(outs)) {
                outs.forEach(e -> {
                    JSONObject e1 = (JSONObject) e;
                    JSONObject orTradeData = e1.getJSONObject("OR_TRADE_DATA");
                    if (!ObjectUtils.isEmpty(orTradeData)) {
                        JSONArray orTradeDataItem = orTradeData.getJSONArray("OR_TRADE_DATA_ITEM");
                        if (!CollectionUtil.isEmpty(orTradeDataItem)) {
                            orTradeDataItem.forEach(vf -> {
                                JSONObject v1 = (JSONObject) vf;
                                String orSerialNo = v1.getString("OR_SERIAL_NO");
                                String message = v1.getString("MESSAGE");
                                for (BankWtczRequestBo bwb1 : v) {
                                    String requestSrl = bwb1.getRequestSrl();
                                    if (Integer.parseInt(requestSrl.substring(requestSrl.length() - 4)) == Integer.parseInt(orSerialNo)) {
                                        log.info("{}: {}", orSerialNo, message);
                                        break;
                                    }
                                }
                            });
                        }
                    }
                });
            }
        });
    }


    private List<BankWtczRequestBo> initData() {
        return Lists.newArrayList(
                new BankWtczRequestBo("20251028158524580001"),
                new BankWtczRequestBo("20251028158524580002"),
                new BankWtczRequestBo("20251028158524580003"),
                new BankWtczRequestBo("20251028158524580004"),
                new BankWtczRequestBo("20251028158524580005"),
                new BankWtczRequestBo("20251028158524580006"),
                new BankWtczRequestBo("20251028158524580007"),
                new BankWtczRequestBo("20251028158524580008"),
                new BankWtczRequestBo("20251028158524580009"),
                new BankWtczRequestBo("20251028158524580010"),
                new BankWtczRequestBo("20251028158524580011"),
                new BankWtczRequestBo("20251028158524580012"),
                new BankWtczRequestBo("20251028158524580013"),
                new BankWtczRequestBo("20251028158524580014"),
                new BankWtczRequestBo("20251028158524580015"),
                new BankWtczRequestBo("20251028158524580016")
        );
    }
}
